<?php
class tag_db extends Model
{
	public $pk = 'tag_id';
	public $table = 'ptx_tag';
	
	var $linker = array(
		'category'=>array(
			'type' => 'hasone',  
			'map' => 'category',
			'mapkey' => 'category_id',
			'ftable' => 'ptx_category',
			'fkey' => 'category_id',
			'enabled' => true 
		)
	);
	
	public function search($page,$pagesize){
		$sort = " ptx_tag.tag_id ASC ";
		return $this->pager($page, $pagesize)->findAllJoin('',$sort,' ptx_tag.*,category.category_name_cn ');
	}
	
	public function getTagsByCategory($catid){
		$rs = pcache('r', 'tag_cache_cat_'.$catid);
		if(!$rs){
			return $this->updateTagsCacheByCategory($catid);
		}
		return $rs;
	}
	
	public function updateTagsCacheByCategory($catid){
		if($rs = $this->findAllJoin(array('category_id'=>$catid),' ptx_tag.display_order ASC ')){
			pcache('w', 'tag_cache_cat_'.$catid,$rs);
			return $rs;
		}
		return FALSE;
	}

}
